package com.atguigu.lease.web.admin.service.impl;

import com.atguigu.lease.common.exception.LeaseException;
import com.atguigu.lease.common.result.ResultCodeEnum;
import com.atguigu.lease.model.entity.*;
import com.atguigu.lease.model.enums.ItemType;
import com.atguigu.lease.web.admin.mapper.*;
import com.atguigu.lease.web.admin.service.*;
import com.atguigu.lease.web.admin.vo.apartment.ApartmentDetailVo;
import com.atguigu.lease.web.admin.vo.apartment.ApartmentItemVo;
import com.atguigu.lease.web.admin.vo.apartment.ApartmentQueryVo;
import com.atguigu.lease.web.admin.vo.apartment.ApartmentSubmitVo;
import com.atguigu.lease.web.admin.vo.fee.FeeValueVo;
import com.atguigu.lease.web.admin.vo.graph.GraphVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * @author liubo
 * @description 针对表【apartment_info(公寓信息表)】的数据库操作Service实现
 * @createDate 2023-07-24 15:48:00
 */
@Service
public class ApartmentInfoServiceImpl extends ServiceImpl<ApartmentInfoMapper, ApartmentInfo>
        implements ApartmentInfoService {
    @Autowired
    private ApartmentInfoMapper apartmentInfoMapper;

    @Autowired
    private GraphInfoMapper graphInfoMapper;

    @Autowired
    private RoomInfoMapper roomInfoMapper;

    @Autowired
    private LabelInfoMapper labelInfoMapper;

    @Autowired
    private FacilityInfoMapper facilityInfoMapper;

    @Autowired
    private FeeKeyMapper feeKeyMapper;

    @Autowired
    private FeeValueMapper feeValueMapper;

    @Autowired
    private GraphInfoService graphInfoService;

    @Autowired
    private ApartmentFacilityService apartmentFacilityService;

    @Autowired
    private ApartmentLabelService apartmentLabelService;

    @Autowired
    private ApartmentFeeValueService apartmentFeeValueService;

    @Override
    public void saveOrUpdateApartment(ApartmentSubmitVo apartmentSubmitVo) {
        boolean isUpdate = apartmentSubmitVo.getId() != null;
        super.saveOrUpdate(apartmentSubmitVo);
        if (isUpdate) {
            //修改要把原来的先删除

            //删除信息
            //公寓配套id
//1.删除图片列表
            LambdaQueryWrapper<GraphInfo> graphQueryWrapper = new
                    LambdaQueryWrapper<>();
            graphQueryWrapper.eq(GraphInfo::getItemType,
                    ItemType.APARTMENT);//设置类型
            graphQueryWrapper.eq(GraphInfo::getItemId,apartmentSubmitVo.getId());
            graphInfoService.remove(graphQueryWrapper);
//2.删除配套列表
            LambdaQueryWrapper<ApartmentFacility> facilityQueryWrapper = new
                    LambdaQueryWrapper<>();
            facilityQueryWrapper.eq(ApartmentFacility::getApartmentId,apartmentSubmitVo.getId());
            apartmentFacilityService.remove(facilityQueryWrapper);
            //公寓标签id

            LambdaQueryWrapper<ApartmentLabel>  labelQueryWrapper =new LambdaQueryWrapper<>();
            labelQueryWrapper.eq(ApartmentLabel::getApartmentId,apartmentSubmitVo.getId());
            apartmentLabelService.remove(labelQueryWrapper);
            //公寓杂费值id
            LambdaQueryWrapper<ApartmentFeeValue>  feeQueryWrapper =new LambdaQueryWrapper<>();
            feeQueryWrapper.eq(ApartmentFeeValue::getApartmentId,apartmentSubmitVo.getId());
            apartmentFeeValueService.remove(feeQueryWrapper);


        }

        //1.插入图片列表
        List<GraphVo> graphVoList = apartmentSubmitVo.getGraphVoList();
        ArrayList<GraphInfo> graphInfos = new ArrayList<>();
        if (!CollectionUtils.isEmpty(graphVoList)){
            for (GraphVo graphVo : graphVoList) {
                GraphInfo graphInfo = new GraphInfo();
                graphInfo.setName(graphVo.getName());
                graphInfo.setUrl(graphVo.getUrl());
                graphInfo.setItemType(ItemType.APARTMENT);
                graphInfo.setItemId(apartmentSubmitVo.getId());
                graphInfos.add(graphInfo);
            }
            graphInfoService.saveBatch(graphInfos);
        }


        //2.插入配套列表
//        List<Long> facilityInfoIds = apartmentSubmitVo.getFacilityInfoIds();
//        if (!CollectionUtils.isEmpty(facilityInfoIds)){
//            ArrayList<ApartmentFacility> apartmentFacilities = new ArrayList<>();
//            for (Long facilityInfoId : facilityInfoIds) {
//                ApartmentFacility apartmentFacility = new ApartmentFacility();
//                apartmentFacility.setApartmentId(apartmentSubmitVo.getId());
//                apartmentFacility.setFacilityId(facilityInfoId);
//            }
//            apartmentFacilityService.saveBatch(apartmentFacilities);
//        }
        List<Long> facilityInfoIdList =
                apartmentSubmitVo.getFacilityInfoIds();
        if (!CollectionUtils.isEmpty(facilityInfoIdList)){
            ArrayList<ApartmentFacility> facilityList = new ArrayList<>();
            for (Long facilityId : facilityInfoIdList) {
                ApartmentFacility apartmentFacility = new ApartmentFacility();
                apartmentFacility.setApartmentId(apartmentSubmitVo.getId());
                apartmentFacility.setFacilityId(facilityId);
                facilityList.add(apartmentFacility);
            }
            apartmentFacilityService.saveBatch(facilityList);
        }

        //3.插入标签列表
        List<Long> voLabelIds = apartmentSubmitVo.getLabelIds();
        if (!CollectionUtils.isEmpty(voLabelIds)){
            ArrayList<ApartmentLabel> labelArrayList = new ArrayList<>();
            for (Long labId : voLabelIds) {
                ApartmentLabel apartmentLabel = new ApartmentLabel();
                apartmentLabel.setApartmentId(apartmentSubmitVo.getId());
                apartmentLabel.setLabelId(labId);
                labelArrayList.add(apartmentLabel);
            }
            apartmentLabelService.saveBatch(labelArrayList);
        }
        //4.插入杂费列表

        List<Long> feeValueIds = apartmentSubmitVo.getFeeValueIds();
        if (!CollectionUtils.isEmpty(feeValueIds)){
            ArrayList<ApartmentFeeValue> feeValueArrayList = new ArrayList<>();
            for (Long labId : feeValueIds) {
                ApartmentFeeValue feeValue = new ApartmentFeeValue();
                feeValue.setApartmentId(apartmentSubmitVo.getId());
                feeValue.setFeeValueId(labId);
                feeValueArrayList.add(feeValue);
            }
            apartmentFeeValueService.saveBatch(feeValueArrayList);
        }
    }

    @Override
    public IPage<ApartmentItemVo> pageApartmentItemByQuery(Page<Object> page, ApartmentQueryVo queryVo) {

        return apartmentInfoMapper.pageApartmentItemByQuery(page, queryVo);
    }

    @Override
    public ApartmentDetailVo getDetailById(Long id) {
//        ApartmentDetailVo detailVo = new ApartmentDetailVo();
//        ApartmentInfo apartmentInfo = this.getById(id);
//        BeanUtils.copyProperties(apartmentInfo, detailVo);
//        //查询图片
//        ArrayList<GraphVo> graphVos = new ArrayList<>();
//        LambdaQueryWrapper<GraphInfo> graphInfoLambdaQueryWrapper = new LambdaQueryWrapper<>();
//        graphInfoLambdaQueryWrapper.eq(GraphInfo::getItemId,id);
//        graphInfoService.list(graphInfoLambdaQueryWrapper).forEach(graphInfo -> {
//            GraphVo graphVo = new GraphVo();
//            graphVo.setName(graphInfo.getName());
//            graphVo.setUrl(graphInfo.getUrl());
//            graphVos.add(graphVo);
//        });
//
//        //查询图片
//        ArrayList<LabelInfo> labelInfos = new ArrayList<>();
//        LambdaQueryWrapper<LabelInfo> labelInfoLambdaQueryWrapper = new LambdaQueryWrapper<>();
//        labelInfoLambdaQueryWrapper.eq(labelInfos::getItemId,id);
//        LabelInfoService.list(labelInfoLambdaQueryWrapper).forEach(graphInfo -> {
//            GraphVo graphVo = new GraphVo();
//            graphVo.setName(graphInfo.getName());
//            graphVo.setUrl(graphInfo.getUrl());
//            graphVos.add(graphVo);
//        });
//        detailVo.setGraphVoList(graphVos);
//        detailVo.setLabelInfoList();
//        detailVo.setFacilityInfoList();
//        detailVo.setFeeValueVoList();
//
//        return detailVo;

        //1.查询ApartmentInfo
        ApartmentInfo apartmentInfo = this.getById(id);
        if (apartmentInfo == null) {
            return null;
        }
//2.查询GraphInfo
        List<GraphVo> graphVoList =
                graphInfoMapper.selectListByItemTypeAndId(ItemType.APARTMENT, id);
//3.查询LabelInfo
        List<LabelInfo> labelInfoList =
                labelInfoMapper.selectListByApartmentId(id);
//4.查询FacilityInfo
        List<FacilityInfo> facilityInfoList =
                facilityInfoMapper.selectListByApartmentId(id);
//5.查询FeeValue
        List<FeeValueVo> feeValueVoList =
                feeValueMapper.selectListByApartmentId(id);
        ApartmentDetailVo adminApartmentDetailVo = new ApartmentDetailVo();
        BeanUtils.copyProperties(apartmentInfo, adminApartmentDetailVo);
        adminApartmentDetailVo.setGraphVoList(graphVoList);
        adminApartmentDetailVo.setLabelInfoList(labelInfoList);
        adminApartmentDetailVo.setFacilityInfoList(facilityInfoList);
        adminApartmentDetailVo.setFeeValueVoList(feeValueVoList);
        return adminApartmentDetailVo;
    }

    @Override
    public void removeApartmentById(Long id) {
        LambdaQueryWrapper<RoomInfo> roomQueryWrapper = new
                LambdaQueryWrapper<>();
        roomQueryWrapper.eq(RoomInfo::getApartmentId, id);
        Long count = roomInfoMapper.selectCount(roomQueryWrapper);
        if (count > 0) {
            throw new
                    LeaseException(ResultCodeEnum.ADMIN_APARTMENT_DELETE_ERROR);
        }

        LambdaQueryWrapper<GraphInfo> graphQueryWrapper = new
                LambdaQueryWrapper<>();
        graphQueryWrapper.eq(GraphInfo::getItemType,
                ItemType.APARTMENT);//设置类型
        graphQueryWrapper.eq(GraphInfo::getItemId,id);
        graphInfoService.remove(graphQueryWrapper);
//2.删除配套列表
        LambdaQueryWrapper<ApartmentFacility> facilityQueryWrapper = new
                LambdaQueryWrapper<>();
        facilityQueryWrapper.eq(ApartmentFacility::getApartmentId,id);
        apartmentFacilityService.remove(facilityQueryWrapper);
        //公寓标签id

        LambdaQueryWrapper<ApartmentLabel>  labelQueryWrapper =new LambdaQueryWrapper<>();
        labelQueryWrapper.eq(ApartmentLabel::getApartmentId,id);
        apartmentLabelService.remove(labelQueryWrapper);
        //公寓杂费值id
        LambdaQueryWrapper<ApartmentFeeValue>  feeQueryWrapper =new LambdaQueryWrapper<>();
        feeQueryWrapper.eq(ApartmentFeeValue::getApartmentId,id);
        apartmentFeeValueService.remove(feeQueryWrapper);

        super.removeById(id);
    }
}




